﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;

namespace UtilZ.Dotnet.Ex.DataStruct
{
    /// <summary>
    /// 异步队列选项
    /// </summary>
    public class AsynQueueOptions
    {
        /// <summary>
        /// 异步队列执行线程名,默认null
        /// </summary>
        public string ThreadName { get; set; } = null;

        /// <summary>
        /// 是否是后台线程,默认true[true:后台线程，false:前台线程]
        /// </summary>
        public bool IsBackground { get; set; } = true;

        /// <summary>
        /// 是否自动启动线程,默认false[true:自动启动线程;false:手动启动线程]
        /// </summary>
        public bool AutoStart { get; set; } = false;

        /// <summary>
        /// 队列容量,默认int.MaxValue
        /// </summary>
        public int Capcity { get; set; } = int.MaxValue;

        /// <summary>
        /// 当队列容量满了的时候，扩容因子
        /// </summary>
        public double CapacityExpansionFactor { get; set; } = 2.0d;

        /// <summary>
        /// 批量处理最大项数
        /// </summary>
        public int BatchCount { get; set; } = 10;

        /// <summary>
        /// 空队列等待超时时间.默认10000毫秒
        /// </summary>
        public int EmptyQueueWaitTimeout { get; set; } = 10000;

        /// <summary>
        /// 当队列中的项数少于批量处理最大项数时的等待时间,单位毫秒,小于等于0无等待.默认10毫秒
        /// </summary>
        public int BatchMillisecondsTimeout { get; set; } = 10;

        /// <summary>
        /// 添加到队列超时时长(当生产者能力大于消费者能力的时候才会有效).
        /// 单位：毫秒，Infinite(-1) 表示无限等待，零表示测试等待句柄的状态并立即返回。.默认1000毫秒
        /// </summary>
        public int EnqueueMillisecondsTimeout { get; set; } = 1000;

        /// <summary>
        /// 队列满了之后,再添加时的处理模式
        /// </summary>
        public ContainerFullMode FullMode { get; set; } = ContainerFullMode.Exception;

        /// <summary>
        /// 数据标记
        /// </summary>
        public object Tag { get; set; }



        /// <summary>
        /// 构造函数
        /// </summary>
        public AsynQueueOptions()
        {

        }


#if CORE

        ///// <summary>
        ///// 当队列满了之后,再添加时的处理模式
        ///// </summary>
        //public System.Threading.Channels.BoundedChannelFullMode FullMode { get; set; } = System.Threading.Channels.BoundedChannelFullMode.DropOldest;

        internal System.Threading.Channels.BoundedChannelOptions ToBoundedChannelOptions()
        {
            var option = new System.Threading.Channels.BoundedChannelOptions(this.Capcity);
            int value = (int)this.FullMode;
            option.FullMode = (System.Threading.Channels.BoundedChannelFullMode)value;
            option.SingleReader = true;
            option.SingleWriter = false;
            return option;
        }
#endif

    }

}
